package org.dolphin.entity.inventory;

import java.io.Serializable;
import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.springframework.util.ObjectUtils;

@Entity
@Table(name = "MS_PRODUCT_TO_CATEGORY")
@AssociationOverrides(value = {
    @AssociationOverride(name = "pk.product", joinColumns = @JoinColumn(name = "PRODUCT_ID")),
    @AssociationOverride(name = "pk.category", joinColumns = @JoinColumn(name = "PRODUCT_CATEGORY_ID"))})
public class ProductCategoryLink implements Serializable {
  private static final long serialVersionUID = -6986144241530496862L;
  private ProductCategoryLinkPk pk;

  public ProductCategoryLink() {
    this(new ProductCategoryLinkPk());
  }

  public ProductCategoryLink(ProductCategoryLinkPk pk) {
    setPk(pk);
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    } else if (obj instanceof ProductCategoryLink) {
      ProductCategoryLink oth = (ProductCategoryLink) obj;
      return ObjectUtils.nullSafeEquals(getPk(), oth.getPk());
    }
    return false;
  }

  @Transient
  public ProductCategory getCategory() {
    return getPk().getCategory();
  }

  @EmbeddedId
  public ProductCategoryLinkPk getPk() {
    return pk;
  }

  @Transient
  public Product getProduct() {
    return getPk().getProduct();
  }

  @Override
  public int hashCode() {
    int hash = ObjectUtils.nullSafeHashCode(getPk());
    return hash;
  }

  public void setPk(ProductCategoryLinkPk pk) {
    this.pk = pk;
  }
}
